package com.supermarket_project.controller;

import com.supermarket_project.common.R;
import com.supermarket_project.pojo.CashGood;
import com.supermarket_project.pojo.FindGoodName;
import com.supermarket_project.pojo.Judge;
import com.supermarket_project.service.CashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/supermarket/cash")
@CrossOrigin //解决了跨域问题
public class CashierController {

    @Autowired
    private CashService cashService;


    @PostMapping("/findbygoodid")
    public R findGoodId(@RequestBody FindGoodName findGoodName){
        CashGood cashGood = cashService.find(findGoodName);
        if (Objects.isNull(cashGood)){
            return R.error("商品无法售卖");
        }
        return R.success(cashGood,"操作成功");
    }

    @PostMapping("/addSerialNumber")
    public R saveCash(@RequestBody List<CashGood> list){
//        List<Judge> judge = cashService.judge(list);
        //给数据加上流水号
        cashService.addSerialNumber(list);
        cashService.saveCash(list);
        return R.success(list,"操作成功");
    }

    @PutMapping("/{serialNumber}")
    public R upDateAffirm(@PathVariable String serialNumber){
        cashService.upDateAffirm(serialNumber);
        return R.success(null,"出售成功");
    }
}